package controllers

import (
	"encoding/json"
	"time"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/services"
)

type StocksController struct {
	BaseController
}

func (c *StocksController) Prepare() {
	//先执行
	c.BaseController.Prepare()
	c.Data["activeSidebarUrl"] = "/stock/records"
}

//盘点记录
func (c *StocksController) Records() {
	c.checkAuthor("stock/records") //需要权限控制

	if c.Ctx.Input.IsPost() {
		c.dataGrid()
	}

	c.display("", "stocks/records_footerjs.html")
}

func (c *StocksController) dataGrid() {

	//获取数据列表和总数
	var params models.StocksQueryParam
	json.Unmarshal(c.Ctx.Input.RequestBody, &params)
	params.Depid = c.curUser.DepartmentId
	data, total := models.StocksPageList(&params)

	c.jsonDataTable(total, data)
}

//新增盘点
func (c *StocksController) Create() {
	c.checkAuthor("stock/records") //需要权限控制
	if c.Ctx.Input.IsPost() {
		var params models.ErpProductsQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Depids = c.curUser.ExtendDepartmentList
		data, total := models.ErpProductsPageList(&params)

		c.jsonDataTable(total, data)
	}
	c.showErpTypeid()

	c.display("", "stocks/create_footerjs.html")
}

//存储盘点信息
func (c *StocksController) Save() {
	c.checkAuthor("stock/records") //需要权限控制
	ser := services.StocksService{}
	products_id, _ := c.GetInt64("products_id")
	typeid, _ := c.GetInt("typeid")
	stocks := new(models.ErpStocks)
	stocks.DepartmentId = c.curUser.DepartmentId
	stocks.OperatorName = c.curUser.RealName
	stocks.Products = &models.ErpProducts{Id: products_id}
	stocks.Typeid = typeid
	stocks.OperatorTime = time.Now().Unix()
	stocks.Auditing = 0
	if id, err := ser.StocksCreateRecords(stocks); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	} else {
		c.jsonResult(enums.JRCodeSucc, "ok", id)
	}
}

//输出商品库存明细
func (c *StocksController) SaveList() {
	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("参数错误")
	}

	if c.Ctx.Input.IsPost() {

		stocks, _ := models.GetStocksById(id)
		if stocks.Id == 0 || stocks.DepartmentId != c.curUser.DepartmentId {
			c.pageError("参数错误")
		}
		var params models.ErpInventoryQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Department = c.curUser.DepartmentId
		params.Products = stocks.Products.Id
		params.Limit = -1
		data, _ := models.ErpInventoryPageList(&params)

		//获取已有的盘存数据
		stocks_data := models.StocksDataList(&models.StocksDataQueryParam{StocksId: stocks.Id})
		stocks_data_inf := make(map[int64]interface{})
		for k, _ := range stocks_data {
			if _, ok := stocks_data_inf[stocks_data[k].Inventory.Id]; !ok {
				stocks_data_inf[stocks_data[k].Inventory.Id] = stocks_data[k]
			}
		}
		c.jsonResult(enums.JRCodeSucc, "ok", map[string]interface{}{"invdata": data, "stocks": stocks, "stocks_data": stocks_data_inf})
	}

	c.display("", "stocks/savelist_footerjs.html")
}

//保存盘存明细
func (c *StocksController) SaveDetail() {
	c.checkAuthor("stock/records") //需要权限控制
	stocks_id, _ := c.GetInt64("stocks_id")
	datatable := c.GetString("datatable")
	ser := services.StocksService{}
	if err := ser.SaveDetail(stocks_id, datatable); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}

//确定损益表
func (c *StocksController) ProfitLoss() {
	c.checkAuthor("stock/records") //需要权限控制
	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("参数错误")
	}

	if c.Ctx.Input.IsPost() {

		stocks, _ := models.GetStocksById(id)
		if stocks.Id == 0 || stocks.DepartmentId != c.curUser.DepartmentId {
			c.pageError("参数错误")
		}
		//获取已有的盘存数据，且数据有变动
		stocks_data := models.StocksDataList(&models.StocksDataQueryParam{StocksId: stocks.Id, IsChange: 1, RelatedSel: []interface{}{"Inventory","Inventory__Snd"}})

		c.jsonResult(enums.JRCodeSucc, "ok", map[string]interface{}{"stocks": stocks, "stocks_data": stocks_data})
	}

	c.display("", "stocks/profitloss_footerjs.html")
}

//生成损益表
func (c *StocksController) SaveProfitLoss() {
	c.checkAuthor("stock/records") //需要权限控制
	ser := services.StocksService{}
	stocks_id, _ := c.GetInt64("stocks_id")
	datatable := c.GetString("datatable")
	if err := ser.SaveProfitLoss(c.curUser.DepartmentId, stocks_id, datatable); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}

//打印盘点
func (c *StocksController) Prints() {

	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("缺少参数")
	}

	c.display("", "stocks/prints_footerjs.html", "shared/layout_print.html")
}

//导出盘点
func (c *StocksController) Exports() {

}

//删除盘点
func (c *StocksController) Delete() {
	c.checkAuthor("stock/records") //需要权限控制
	id, _ := c.GetInt64("id")
	if id == 0 {
		c.jsonResult(enums.JRCodeFailed, "缺少参数", "")
	}
	ser := services.StocksService{}

	if err := ser.StocksDelete(id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}

//盘存打印
func (c *StocksController) Printer() {
	c.checkAuthor("stock/printer") //需要权限控制


	c.Data["activeSidebarUrl"] = "/stock/printer"
	c.display("", "stocks/printer_footerjs.html")
}